const router = require('koa-router')()
const jwt = require('jsonwebtoken')
router.prefix('/users')
const secret = 'secret'
//登陆
router.post('/login', (ctx) => {
  const { username, password } = ctx.request.body
  const user = global.users.find(item => {
    return item.username == username && item.password == password
  })
  if (user) {
    const token = jwt.sign({ id: user.id, username: user.username }, secret, {
      expiresIn: 5 * 60
    })
    ctx.body = {
      code: 0,
      msg: '登录成功',
      data: {
        token
      },
      user: user
    }
  } else {
    ctx.body = {
      code: 1,
      msg: '用户名或密码错误',
      data: null
    }
  }
})

//修改密码接口
router.post('/emit', async (ctx) => {
  //接收前端传过来的数据
  const { newPassword, password, username } = ctx.request.body
  //修改密码
  const user = global.users.find(item => {
    return item.username == username
  })
  // console.log(user, 1123123123123)
  // console.log(username, 121231321213)
  user.password = newPassword
  ctx.body = {
    code: 0,
    msg: '修改成功',
    data: user.password
  }

})

//获取设置页面班级设置表格数据
router.post('/class', async (ctx) => {
  //获取数据
  ctx.body = {
    code: 0,
    msg: '获取成功',
    data: global.usersScore.banjidata
  }

})

//设置页面班级设置新增学生
router.post('/addstuse', async (ctx) => {
  // console.log(ctx.request.body, 123123123123123)
  global.usersScore.banjidata.push(ctx.request.body)
  ctx.body = {
    code: 0,
    msg: '添加成功',
    data: null
  }

})

//设置页面班级页面删除
router.post('/delstuse', async (ctx) => {
  // console.log(ctx.request.body.ids, 123123123123123)
  //删除
  global.usersScore.banjidata = global.usersScore.banjidata.filter(item => item.id != ctx.request.body.ids)
  ctx.body = {
    code: 0,
    msg: '删除成功',
    data: null
  }

})

//设置页面班级设置设置接口
router.post('/setstuse', async (ctx) => {
  const { names, zhanghao, password, id } = ctx.request.body
  const res = global.usersScore.banjidata.find(item => item.id == id)
  // console.log(names, zhanghao, password, id , "1212123123123123123")
  // console.log(res, 123123123123123)
  res.name = names
  res.Account = zhanghao
  res.password = password
  ctx.body = {
    code: 0,
    msg: '设置成功',
    data: null
  }

})

//设置页面管理员设置班级管理数据接口
router.post('/guanbanjidatas', async (ctx) => {
  ctx.body = {
    code: 0,
    msg: '获取成功',
    data: global.usersScore.guanbanji
  }

})


//管理员设置班级管理新增接口
router.post('/addguan', async (ctx) => {
  // console.log(ctx.request.body, 123123123123123)
  global.usersScore.guanbanji.push(ctx.request.body)
  ctx.body = {
    code: 0,
    msg: '添加成功',
    data: global.usersScore.guanbanji
  }
})

//设置也页面管理员班级管理删除
router.post('/delguan', async (ctx) => {
  //删除
  global.usersScore.guanbanji = global.usersScore.guanbanji.filter(item => item.id != ctx.request.body.ids)
  ctx.body = {
    code: 0,
    msg: '删除成功',
    data: global.usersScore.guanbanji
  }
})
//设置页面管理员教师管理删除接口
router.post('/deljiaoshi', async (ctx) => {
  //删除
  global.usersScore.guanjiaoshi = global.usersScore.guanjiaoshi.filter(item => item.id != ctx.request.body.ids)
  ctx.body = {
    code: 0,
    msg: '删除成功',
    data: global.usersScore.guanjiaoshi
  }
})
//设置页面管理员班级设置接口
router.post('/gbsz', async (ctx) => {
  const { id, grade } = ctx.request.body
  global.usersScore.guanbanji.forEach(item => {
    if (item.id == id) {
      item.grade = grade
    }
  })
  //设置
  ctx.body = {
    code: 0,
    msg: '设置成功',
    data: global.usersScore.guanbanji
  }
})
//设置页面管理员教师数据
router.post('/jiaoshidata', async (ctx) => {
  //数据
  ctx.body = {
    code: 0,
    msg: '获取成功',
    data: global.usersScore.guanjiaoshi
  }
})

//设置页面管理员教师新增接口
router.post('/addTeacher', async (ctx) => {
  global.usersScore.guanjiaoshi.push(ctx.request.body)
  //数据
  ctx.body = {
    code: 0,
    msg: '添加成功',
    data: global.usersScore.guanjiaoshi
  }
})

//设置页面管理员教师设置接口
router.post('/setTeacher', async (ctx) => {
  const { id, mingzi, zhanghaos, passwords, namess, phone } = ctx.request.body
  global.usersScore.guanjiaoshi.forEach(item => {
    if (item.id == id) {
      item.mingzi = mingzi
      item.zhanghaos = zhanghaos
      item.passwords = passwords
      item.namess = namess
      item.phone = phone
    }
  })
  //设置
  ctx.body = {
    code: 0,
    msg: '设置成功',
    data: global.usersScore.guanjiaoshi
  }
})
//设置页面班级批量删除接口
router.post('/delAll', async (ctx) => {
  // console.log(ctx.request.body, 123123123123123)
  ctx.request.body.forEach(item => {
    global.usersScore.banjidata = global.usersScore.banjidata.filter(item2 => item2.id != item.id)
  })
  ctx.body = {
    code: 0,
    msg: '删除成功',
    data: global.usersScore.banjidata
  }
})

//设置页面管理员班级批量删除
router.post('/delAllguan', async (ctx) => {
  // console.log(ctx.request.body, 123123123123123)
  ctx.request.body.forEach(item => {
    global.usersScore.guanbanji = global.usersScore.guanbanji.filter(item2 => item2.id != item.id)
  })
  ctx.body = {
    code: 0,
    msg: '删除成功',
    data: global.usersScore.guanbanji
  }
})
//设置页管理员教师面批量删除
router.post('/delAllTeacher', async (ctx) => {
  console.log(ctx.request.body, 123123123123123)
  ctx.request.body.forEach(item => {
    global.usersScore.guanjiaoshi = global.usersScore.guanjiaoshi.filter(item2 => item2.id != item.id)
  })
  ctx.body = {
    code: 0,
    msg: '删除成功',
    data: global.usersScore.guanjiaoshi
  }
})




//获取作品信息
router.get('/works', (ctx) => {
  const { author } = ctx.query
  console.log(author)
  let lists = []

  global.list.forEach(item => {
    if (item.author == author) {
      lists.unshift(item)
    }
  })
  if (lists.length > 0) {
    ctx.body = {
      code: 0,
      msg: '获取成功',
      data: lists
    }
  } else {
    ctx.body = {
      code: 1,
      msg: '该用户没有作品',
      data: null
    }
  }
})

//分享到个人公开 班级
router.post('/shareUser', (ctx) => {
  const data = ctx.request.body
  console.log(data)
  let id = data.id
  const posiA = data.posiA
  //分享到个人公开
  if(!posiA){
    global.list2.unshift(data)
    ctx.body = {
      code: 0,
      msg: '分享到个人公开成功',
      data: global.list2
    }
  }
  //分享到班级
  else if (posiA == '1' || posiA == '2') {
    console.log('11111111111111')
    data.catalogue='分享区'
    global.list4.unshift(data)
    let user= global.list.find(item => {
      return item.id == id
    })
    user.catalogue = '分享区'
    ctx.body = {
      code: 0,
      msg: '分享到班级成功',
      data: global.list4
    }
  } else {
    ctx.body = {
      code: '1',
      msg: '分享失败'
    }
  }


})

//获取个人公开信息
router.get('/getUser', (ctx) => {
  const { id } = ctx.query
  console.log(id)
  let list2 = []
  global.list2.forEach(item => {
    if (item.userId == id) {
      list2.unshift(item)
    }
  })
  if (list2.length > 0) {
    ctx.body = {
      code: 0,
      msg: '获取成功',
      data: list2
    }
  } else {
    ctx.body = {
      code: 1,
      msg: '无信息',
    }
  }

})

//分享到社区
router.post('/ShareComm', (ctx) => {
  const data = ctx.request.body
  data.catalogue = '分享区'
  console.log(data)
  let user = global.list.find(item => {
    return item.id == data.id
  })
  user.catalogue='分享区'
  global.list3.unshift(data)
  ctx.body = {
    code: 0,
    msg: '分享成功',
    data: global.list3
  }
})

//获取社区信息
router.get('/ShareComm', (ctx) => {
  // console.log(111111111)
  if (global.list3.length > 0) {
    ctx.body = {
      code: 0,
      msg: '获取成功',
      data: global.list3
    }
  } else {
    ctx.body = {
      code: 1,
      msg: '无信息',
    }
  }

})


// 添加编辑区
router.post('/addList', (ctx) => {
  // 尝试从请求体中获取数据  
  const requestBody = ctx.request.body;
  console.log('requestBody: ', requestBody);

  // 确保请求体是一个对象  
  if (typeof requestBody === 'object' && requestBody !== null) {
    // 将对象添加到数组中  
    global.list.unshift(requestBody);
    console.log('global.list: ', global.list);
    // 查找个人的数据
    const userList = global.list.filter(item => {
      if (item.userId == requestBody.id) {
        return item
      }
    })
    console.log('userList: ', userList);
    // 设置响应状态码和响应体  
    ctx.status = 200;
    ctx.body = {
      message: '添加成功',
      data: userList // 可选：返回当前数组的状态  
    };
  } else {
    // 如果请求体不是对象，则返回错误  
    ctx.status = 400;
    ctx.body = {
      message: 'Invalid request body'
    };
  }
})

// TDOO 获取对应编辑区内容

router.get('/editList', async (ctx) => {
  const { id, dataId } = ctx.query;
  if (Number(id) === 1) {
    const userList = global.list2.filter(item => {
      if (item.userId == Number(ctx.query.dataId)) {
        return item
      }
    })
    console.log('userList: ', userList);
    ctx.body = {
      code: 0,
      msg: '获取个人公开成功',
      data: userList
    }
  } else if (Number(id) === 2) {
    const userList3 = global.list3.filter(item => {
      if (item.userId == Number(ctx.query.dataId)) {
        console.log('ctx.query.userId: ', ctx.query.dataId);
        return item
      }
    })
    const userList4 = global.list4.filter(item => {
      if (item.userId == Number(ctx.query.dataId)) {
        console.log('ctx.query.userId: ', ctx.query.dataId);
        return item
      }
    })
    const userList=[...userList3, ...userList4]
    console.log('userList: ', userList);
    ctx.body = {
      code: 0,
      msg: '获取分享成功',
      data: userList
    }
  } else if (Number(id) === 3) {
    const userList = global.list.filter(item => {
      if (item.userId == Number(ctx.query.dataId)) {
        console.log('ctx.query.userId: ', ctx.query.dataId);
        return item
      }
    })
    console.log('userList: ', userList);
    ctx.body = {
      code: 0,
      msg: '获取编辑成功',
      data: userList
    }
  } else if (Number(id) === 7) {
    const userList = global.list2.filter(item => {
      if (item.userId == Number(ctx.query.dataId)) {
        console.log('ctx.query.userId: ', ctx.query.dataId);
        return item
      }
    })
    console.log('userList: ', userList);
    ctx.body = {
      code: 0,
      msg: '获取个人公开成功',
      data: userList
    }
  }
})


// 退出登陆
router.get('/logout', (ctx) => {
  ctx.body = {
    code: 0,
    msg: '退出成功',
    data: null
  }
})




//点赞
router.post('/addLike', (ctx) => {
 const {id,like,fromId,type}= ctx.request.body
 console.log(ctx.request.body)
 console.log(id,like,fromId,type)
 console.log('11111111111111111111111111111111')
 if(type=='6'){
   global.list3.forEach(item => {
    if (item.id == id){
      if(item.LikeUser){
        //判断里面是否有fromId
        if (item.LikeUser.includes(fromId)) {
          //如果存在则删除
          item.like--
          item.LikeUser = item.LikeUser.filter(item => item != fromId)
          ctx.body = {
            code: 1,
            msg: '取消成功',
            data: item
          }
        } else {
          item.like++
          item.LikeUser.unshift(fromId)
          ctx.body = {
            code: 0,
            msg: '点赞成功',
            data: item
          }
        }
      } else {
        item.like++
        item.LikeUser = [fromId]
        ctx.body = {
          code: 0,
          msg: '点赞成功',
          data: item
        }
      }
    }
  })
 }else if(type=='1'){
  global.list2.forEach(item => {
    if (item.id == id){
      if(item.LikeUser){
        //判断里面是否有fromId
        if(item.LikeUser.includes(fromId)){
          //如果存在则删除
          item.like--
          item.LikeUser = item.LikeUser.filter(item => item != fromId)
          ctx.body = {
            code: 1,
            msg: '取消成功',
            data: item
           }
        }else{
          item.like++
          item.LikeUser.unshift(fromId)
          ctx.body = {
            code: 0,
            msg: '点赞成功',
            data: item 
           } 
        }   
      }else{
        item.like++
        item.LikeUser = [fromId]
        ctx.body = {
          code: 0,
          msg: '点赞成功',
          data: item
         }
      }
    }
  })
 }else if(type=='4'||type=='5'){
  global.list4.forEach(item => {
    if (item.id == id){
      if(item.LikeUser){
        //判断里面是否有fromId
        if(item.LikeUser.includes(fromId)){
          //如果存在则删除
          item.like--
          item.LikeUser = item.LikeUser.filter(item => item != fromId)
          ctx.body = {
            code: 1,
            msg: '取消成功',
            data: item
           }
        }else{
          item.like++
          item.LikeUser.unshift(fromId)
          ctx.body = {
            code: 0,
            msg: '点赞成功',
            data: item
           }
        }
      }else{
        item.like++
        item.LikeUser = [fromId]
        ctx.body = {
          code: 0,
          msg: '点赞成功',
          data: item
         }
      }
    }
  })
 }
 
  
})

//评论
router.post('/addComment', (ctx) => {
  const {id,fromId,rate,comment,fromName,RwId,type}= ctx.request.body
  console.log(id,fromId,rate,comment,fromName,RwId)
  if(type=='6'){
     global.list3.forEach(item => {
     if (item.id == id){
       if(item.commentList){
      
           item.comment++
           item.commentList.unshift({
             fromId,
             rate,
             comment,
             fromName,
             RwId
           })
           ctx.body = {
             code: 0,
             msg: '评论成功',
             data: global.list3
            }
       }else{
         item.comment++
         item.commentList = [{
           fromId,
           rate,
           comment,
           fromName,
           RwId
         }]
         ctx.body = {
           code: 0,
           msg: '评论成功',
           data:global.list3
          }
        }
      }
    })
  } else if (type == '1') {
    global.list2.forEach(item => {
      if (item.id == id){
        if(item.commentList){
       
            item.comment++
            item.commentList.unshift({
              fromId,
              rate,
              comment,
              fromName,
              RwId
            })
            ctx.body = {
              code: 0,
              msg: '评论成功',
              data: global.list3
             }
        }else{
          item.comment++
          item.commentList = [{
            fromId,
            rate,
            comment,
            fromName,
            RwId
          }]
          ctx.body = {
            code: 0,
            msg: '评论成功',
            data: global.list3
          }
        }
      }
    })
  } else if (type == '4' || type == '5') {
    global.list4.forEach(item => {
      if (item.id == id){
        if(item.commentList){
       
            item.comment++
            item.commentList.unshift({
              fromId,
              rate,
              comment,
              fromName,
              RwId
            }) 
            ctx.body = {
              code: 0,
              msg: '评论成功',
              data: global.list3
             }
        }else{  
          item.comment++
          item.commentList = [{
            fromId,
            rate, 
            comment,
            fromName, 
            RwId
          }]   
          ctx.body = { 
            code: 0,  
            msg: '评论成功',
            data: global.list3
          }
        }
      }
    })
  }
})  
   
//删除评论
router.post('/delComment', (ctx) => {
  const { id, RwId,type} = ctx.request.body
  console.log(id,RwId,type, '0000000000000000000000000000000000000')
  if(type=='6'){
     global.list3.forEach(item => {
    if (item.id == id) { 
      item.comment--
      item.commentList = item.commentList.filter(item => item.RwId != RwId)
    }   
  })     
  }else if(type=='1'){
    global.list2.forEach(item => {
      if (item.id == id) {
        item.comment--
        item.commentList = item.commentList.filter(item => item.RwId != RwId)
      } 
    })
  }else if(type=='4' || type=='5'){
    global.list4.forEach(item => {
      if (item.id == id) {
        item.comment--
        item.commentList = item.commentList.filter(item => item.RwId != RwId)
      }
    })
  }
  ctx.body = {
    code: 0,
    msg: '删除成功',
    data: global.list3
  }
})

//删除乐谱
router.post('/delShareA', (ctx) => {
  let { id, type } = ctx.request.body
  console.log(id, type)
  if (type == '6') {
    global.list3 = global.list3.filter(item => item.id != id)
  } else if (type == '1') {
    global.list2 = global.list2.filter(item => item.id != id)
  } else if (type == '3') {
    global.list = global.list.filter(item => item.id != id)
  }

  ctx.body = {
    code: 0,
    msg: '删除成功',
  }
})


// 获取个人信息
router.post('/presonal/:id', (ctx) => {
  const { id } = ctx.params; 
  console.log(id, 11111111111)
  const userList = global.users.filter(item => {
    if (item.id == Number(id)) {
      return item
    }
  })
  console.log('userList: ', userList);
  ctx.body = {
    code: 0,
    msg: '获取个人信息成功',
    data: userList
  }
})


//获取分组
router.get('/ListSelect',(ctx)=>{
  //路由传参
  const {type} = ctx.query
  console.log(type)
  if(type =='社区分享'||type=='6'){
    ctx.body = {
      code: 0,
      msg: '获取分组成功',
      data: global.listType.CommData
    }
  }else{
    ctx.body={
      code: 0,
      msg: '敬请期待',
    }
  }
})

//新增分组
router.post('/addListSelect',(ctx)=>{
 const {label,value,type}=ctx.request.body
 console.log(label,value,type)
 if(type=='6'){
  global.listType.CommData.push({
    label,
    value,
    id:new Date().getTime()
  })
 }
  ctx.body={
    code: 0,
    msg: '新增分组成功',
  }
})

//删除分组
router.post('/delListSelect',(ctx)=>{
  const {id,type}=ctx.request.body
  console.log(id,type)
  if(type=='6'){
    global.listType.CommData=global.listType.CommData.filter(item=>{
      if(item.id!=id){
        return item
      }
    })
  }
  ctx.body={
    code: 0,
    msg: '删除分组成功',
  }
})


module.exports = router
